package com.ruitu.health_1.mvp.presenter;

import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.dao.bean.OrderDetail;
import com.ruitu.health_1.dao.bean.Pay;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;
import com.ruitu.health_1.dao.bean.SubCommit;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.ui.CommitOrderActivity;
import com.ruitu.health_1.ui.OrderDetailsActivity;
import com.ruitu.health_1.ui.PayOrderActivity;
import com.ruitu.health_1.view.NoCacheLoadingFragment;
import com.ruitu.router_module.bean.BaseResult2;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.cart.CouponList;
import com.ruitu.router_module.bean.user.Address;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class OrderPresenter {
    public static class GenerateP extends BasePresenter<CommitOrderActivity, OrderModel.GenerateM> {
        public Disposable generate(RequestBody body) {
            return m.generate(body)
                    .subscribe(new Consumer<BaseResult2<Pay>>() {
                        @Override
                        public void accept(BaseResult2<Pay> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setCommitSuc(baseResult2.getData());
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable subOrder(RequestBody body) {
            return m.subOrder(body)
                    .subscribe(new Consumer<BaseResult2<SubCommit>>() {
                        @Override
                        public void accept(BaseResult2<SubCommit> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                Pay pay = new Pay();
                                pay.setOrderId(baseResult2.getData().getId());
                                pay.setPayAmount(baseResult2.getData().getPayAmount() + "");
                                Address address = new Address();
                                address.setId(baseResult2.getData().getId());
                                address.setProvince(baseResult2.getData().getReceiverProvince());
                                address.setCity(baseResult2.getData().getReceiverCity());
                                address.setRegion(baseResult2.getData().getReceiverRegion());
                                v.setCommitSuc(pay);
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable getCanUseCoupon(String cartItemId, String memberId) {
            return m.getCanUseCoupon(cartItemId, memberId)
                    .subscribe(new Consumer<BaseResult2<List<CouponList>>>() {
                        @Override
                        public void accept(BaseResult2<List<CouponList>> listBaseResult2) throws Exception {
                            v.hideProgress();
                            if (listBaseResult2.isSuccess()) {
                                v.getUserCoupon(listBaseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable lock(RequestBody body) {
            return m.lock(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            if (baseResult2.isSuccess()) {
                                v.setCoupon();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }

        public Disposable unLock(RequestBody body) {
            return m.unLock(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            if (baseResult2.isSuccess()) {
                                v.unLockSuc();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }

        public Disposable memberGoldLock(RequestBody body) {
            return m.memberGoldLock(body)
                    .subscribe(new Consumer<BaseResult2<String>>() {
                        @Override
                        public void accept(BaseResult2<String> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setGold(baseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable memberGoldUnLock(RequestBody body) {
            return m.memberGoldUnLock(body)
                    .subscribe(new Consumer<BaseResult2<String>>() {
                        @Override
                        public void accept(BaseResult2<String> baseResult2) throws Exception {
v.setGoldUnLock(baseResult2.getData());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }

    }

    public static class OrderP extends BasePresenter<NoCacheLoadingFragment, OrderModel.OrderM> {
        public Disposable orderList(int pageNum, int pageSize, String memberId, String status) {
            return m.orderList(pageNum, pageSize, memberId, status)
                    .subscribe(new Consumer<BaseResult2<PageModel2<StoreOrderItemList>>>() {
                        @Override
                        public void accept(BaseResult2<PageModel2<StoreOrderItemList>> pageModel2) throws Exception {
                            v.hideProgress();
                            if (pageModel2.isSuccess()) {
                                v.setData(pageModel2.getData().getList());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            throwable.printStackTrace();
                            v.hideProgress();
                        }
                    });
        }

        public Disposable payOldorder(RequestBody body) {
            return m.payOldorder(body)
                    .subscribe(new Consumer<BaseResult2<Pay>>() {
                        @Override
                        public void accept(BaseResult2<Pay> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setWaitPayData(baseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }


        public Disposable confirmReceipt(RequestBody body) {
            return m.confirmReceipt(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.requestSuc();
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable cancelOrder(RequestBody body) {
            return m.cancelOrder(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.requestSuc();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

    public static class PayOrderP extends BasePresenter<PayOrderActivity, OrderModel.PayOrderM> {
        public Disposable balancePay(RequestBody body) {
            return m.balancePay(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.paySuc();
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }


    }

    public static class OrderDetailsP extends BasePresenter<OrderDetailsActivity, OrderModel.OrderM> {
        public Disposable getOrder(String memberId, String orderId) {
            return m.getOrder(memberId, orderId)
                    .subscribe(new Consumer<BaseResult2<OrderDetail>>() {
                        @Override
                        public void accept(BaseResult2<OrderDetail> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.setData(baseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }

        public Disposable getOrderItem(String memberId, String orderItemId) {
            return m.getOrderItem(memberId, orderItemId)
                    .subscribe(new Consumer<BaseResult2<OrderDetail>>() {
                        @Override
                        public void accept(BaseResult2<OrderDetail> orderDetailBaseResult2) throws Exception {
                            v.hideProgress();
                            if (orderDetailBaseResult2.isSuccess()) {
                                v.setData(orderDetailBaseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable confirmReceipt(RequestBody body) {
            return m.confirmReceipt(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {

                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable cancelOrder(RequestBody body) {
            return m.cancelOrder(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {

                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }

}
